<?php
class search
{
	function convertUnicode($p_strText)
	{
		$char_unicode = array(
            '#ạ|á|à|ả|ã|â|ậ|ấ|ầ|ẩ|ẫ|ă|ặ|ắ|ằ|ẳ|ẫ#i',			
            '#ê|ẹ|é|è|ẻ|ẽ|ế|ề|ể|ễ|ệ#i',			
            '#ọ|ộ|ổ|ỗ|ố|ồ|ô|ó|ò|ỏ|õ|ơ|ợ|ớ|ờ|ở|ỡ#i',	
            '#ụ|ư|ứ|ừ|ử|ữ|ự|ú|ù|ủ|ũ#i',
            '#ị|í|ì|ỉ|ĩ#i',			
            '#ỵ|ý|ỳ|ỷ|ỹ#i',			
            '#đ#i',
			'#[\\{\\}]#i'
						
      	);
      	$char_EN = array( 'a','e','o','u','i','y','d',"");			
		$strResult = preg_replace($char_unicode,$char_EN,strtolower($p_strText));
		return trim($strResult);
	}
	public function getContentToAPIGoogle($strKeyword,$page=0)
	{	
		try
		{	
			$page = max(0,min(($page-1)*8,60));			
			$strURL = "http://www.google.com.vn/uds/GwebSearch?context=0&lstkp=0&rsz=large&hl=vi&gss=.com&sig=26298449ab1a705c6b1eb52979854b57&start=". $page."&q=". urlencode($strKeyword) ."&key=ABQIAAAANg4xcD8uUvXBJG6h_0UnJxTcPR7htbIYpXmQXRA1f9ZD8hWkFhSSbHSEz6UldzWAftGIDyJP8C8S4Q&v=1.0";						
			$arrResult = json_decode(@file_get_contents($strURL));	
			$arrData = array(array(),0);						
			if(is_object($arrResult) ) 
				if(is_object($arrResult->responseData))
					if(is_array($arrResult->responseData->results))
						$arrData = array($arrResult->responseData->results,@$arrResult->responseData->cursor->estimatedResultCount);
					
		}
		catch(Exception $e) 
		{
			die('event');
		}		
		return $arrData;	
	}
	public function getPagesLinks( $p_intTotal, $p_intPage, $p_intLimit, $p_strUrl, $p_strNameEvent='',$p_strGoogle='') 
	{	
		$p_intTotal = min(63,$p_intTotal);
		if($p_intTotal ==0) return array('','');
		$p_intPage				= max(1,$p_intPage);		
		$p_intLimit 			= (int) max( $p_intLimit, 1 );	
		$p_intLimitStart 		= (int) max( ($p_intPage-1)*($p_intLimit)+1, 0 );
		$p_intLimitEnd			= (int) min( ($p_intPage*$p_intLimit), $p_intTotal);
		$intTotalPages 			= ceil( $p_intTotal / $p_intLimit );
		
		$strResult = "từ <strong>$p_intLimitStart</strong> đến <strong>$p_intLimitEnd</strong> trong số <strong>".number_format(min(63,$p_intTotal), 0, ',', '.')."</strong>";
		if($intTotalPages < 2)
			return array('',$strResult);
		
		if ($p_intLimit > $p_intTotal) 
		{
			$p_intLimitStart = 0;
		}	
		if($p_strGoogle!='')
			$intTotalPages = min($intTotalPages,8);	
		//echo $intTotalPages; exit;
		$strHtml 			= '<ul>';
		// so trang hien thi
		$intDispPages 		= 8;
		// tong so trang co duoc
		
		// trang hien tai dang xem
		$intPageCurrent 	= ceil(($p_intLimitStart +1) / $p_intLimit );
		
		// trang duoc hien thi dau tien
		$intStartLoop 		= (floor(($intPageCurrent - 1)/ $intDispPages)) * $intDispPages + 1;
		if ( $intPageCurrent > floor($intDispPages/2) )
			$intStartLoop = $intPageCurrent - floor($intDispPages/2);
		// trang duoc hien thi cuoi cung
		
		$intStopLoop = $intStartLoop + $intDispPages - 1;
		if ($intStopLoop > $intTotalPages) 		
		{
			$intStopLoop = $intTotalPages;
		}
		if ($intPageCurrent > 1) 
		{
			//$intRowStart = ($intPageCurrent - 2) * $p_intLimit;
			$strUrl	 = str_replace('%s%',($intPageCurrent-1),$p_strUrl);			
			$strHtml .= "<li class=\"rate\"><a href=\"$strUrl\" $p_strNameEvent> «</a></li>";
		} 		
		for ($i=$intStartLoop; $i <= $intStopLoop; $i++) 
		{
			//$intRowStart = ($i - 1) * $p_intLimit;
			if ($i == $intPageCurrent) 
			{
				
				$strHtml .= "<li class=\"rate\"><a href=\"#\" class=\"rate_active\">$i</a></li>";
			} 
			else 
			{
				$strUrl	 = str_replace('%s%',$i,$p_strUrl);			
				$strHtml .= "<li class=\"rate\"><a href=\"$strUrl\" $p_strNameEvent>$i</a></li>";
			}
		}
		if ($intPageCurrent < $intTotalPages) 
		{
			//$intRowStart 	= $intPageCurrent * $p_intLimit;
			$strUrl	 = str_replace('%s%',($intPageCurrent+1),$p_strUrl);
			$strHtml .= "<li class=\"rate\"><a href=\"$strUrl\" $p_strNameEvent>»</a></li>";
		}
		elseif($p_strGoogle!='')
		{
			
			$strHtml .= "<li class=\"rate\"><a target=\"_blank\"  href=\"$p_strGoogle\">Xem tiếp »</a></li>";
		}		
		$strHtml.="</ul>";
		
		return array($strHtml,$strResult);
	}
}
?>